package com.xl.juc.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步回调 CompletableFuture
 * 异步执行
 * 成功回调
 * 失败回调
 * @author Charley
 * @create 2021-01-24
 */
public class completableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //发起一个异步请求，无返回值
       /* CompletableFuture<Void> completableFuture=  CompletableFuture.runAsync(()->{

        });*/
        //发起一个异步请求,有返回值
        CompletableFuture<Integer> completableFuture=  CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+" supplyAsync");
            int i=10/0;
            return 1024;
        });
        Integer integer = completableFuture.whenComplete((t, u) -> {
            System.out.println("t->" + t);//正常返回结果
            System.out.println("u->" + u);//错误信息 java.util.concurrent.CompletionException:
        }).exceptionally((e) -> {
            System.out.println(e.getMessage());
            return 500;
        }).get();
        System.out.println(integer);
    }
}
